package com.tnsw.noxnox.settopbox;

import com.tnsw.coreutils.AsyncManager;
import com.tnsw.noxnox.R;
import com.tnsw.noxnox.R.layout;
import com.tnsw.noxnox.R.menu;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

public class STBTVScreen extends Activity implements OnErrorListener, OnPreparedListener, OnInfoListener, OnBufferingUpdateListener {
	
	private AsyncManager asyncManager;
	private String tag = "STBTVScreen";
	private VideoView videoView;
	private MediaController mc;
	private RelativeLayout layoutVideo;
	
	private String currentUrl = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.asyncManager = AsyncManager.getInstance();
		
		String orientation = this.getIntent().getStringExtra("orientation");
		Log.d(tag, "orientation=" + orientation);
		if (orientation.equals("landscape")) {
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		} else if (orientation.equals("portrait")){
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		} else if (orientation.equals("sensor")) {
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		}
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.activity_stbtvscreen);
		
		this.initialize();
		
		STBService stb = STBService.getInstance();
		if (stb != null) {
			stb.registerTVScreen(this);
		} else {
			Log.e(tag, "STBService is not started!!");
			this.asyncManager.appendNetworkLog("STBService is not started!!");
		}
		
		this.currentUrl = this.getIntent().getStringExtra("url");
		Log.d(tag, "Received url=" + this.currentUrl);
		this.asyncManager.appendNetworkLog("Received url=" + this.currentUrl);
		
		if (this.currentUrl != null) {
			this.doPlay(this.currentUrl);	
		} else {
			this.asyncManager.appendNetworkLog("url is null, cannot play on TV");
		}
		
	}
	
	private void initialize() {
		this.videoView = (VideoView) this.findViewById(R.id.STBTVScreen_videoView);
		this.layoutVideo = (RelativeLayout) this.findViewById(R.id.STBTVScreen_layoutVideo);
	}
	
	public void doPlay(String url) {
		this.asyncManager.appendNetworkLog("doPlay is called, url=" + url);
		if (this.videoView.isPlaying() == true) {
			this.videoView.stopPlayback();
		}
		
		this.currentUrl = url;
		Uri uri = Uri.parse(url);
		this.videoView.setVideoURI(uri);
		this.videoView.setOnErrorListener(this);
		this.videoView.setOnPreparedListener(this);
		this.mc = new MediaController(this);
		this.videoView.setMediaController(this.mc);
		this.mc.show();
		
		this.videoView.start();
		
		Log.d(tag, "Playing " + url);
		this.asyncManager.appendNetworkLog("Playing " + url);
	}
	
	private void recreateVideoview() {
		Log.d(tag, "recreating new VideoView");
		this.asyncManager.appendNetworkLog("recreating new VideoView");
		RelativeLayout.LayoutParams oldLayout = (RelativeLayout.LayoutParams) this.videoView.getLayoutParams();
		Log.d(tag, "old=" + oldLayout.alignWithParent);
		oldLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		oldLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		oldLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		oldLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		
		RelativeLayout.LayoutParams newLayout = new RelativeLayout.LayoutParams(this.videoView.getLayoutParams());
		newLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		newLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		newLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		newLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		Log.d(tag, "new=" + newLayout.alignWithParent);
		
		this.videoView.stopPlayback();
		this.videoView.setVisibility(View.GONE);
		this.layoutVideo.removeView(this.videoView);
		
		this.videoView = new VideoView(this);
		Uri uri = Uri.parse(this.currentUrl);
		this.videoView.setVideoURI(uri);
		this.videoView.setOnErrorListener(this);
		this.videoView.setOnPreparedListener(this);
		this.videoView.setMediaController(this.mc);
		
		this.mc.show(5000);
		this.layoutVideo.addView(this.videoView);
		
		this.videoView.start();
		
		this.videoView.setLayoutParams(newLayout);
		Log.d(tag, "done creating VideoView");
		this.asyncManager.appendNetworkLog("done creating VideoView");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.stbtvscreen, menu);
		return true;
	}

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		Log.d(tag, "onBufferingUpdate(percent=" + percent + ")");
		this.asyncManager.appendNetworkLog("onBufferingUpdate(percent=" + percent + ")");
	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		Log.d(tag, "played....");
		mp.setOnBufferingUpdateListener(this);
		mp.setOnInfoListener(this);
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		Log.d(tag, "onError(what=" + what + ", extra=" + extra + ")");
		this.asyncManager.appendNetworkLog("onError(what=" + what + ", extra=" + extra + ")");
		//this.currentVideoPosition = this.videoView.getCurrentPosition();
		Log.d(tag, "currentPosition=" + this.videoView.getCurrentPosition());
		
		this.recreateVideoview();
		
		return true;
	}

}
